from isq import isq
from ezQpy import *
import numpy as np
import json

#a = [[0.5+0.8660254j,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
a = np.diag([np.exp(1j*val) for val in [0,0,0,2,0,0,2,0,0,2,0,0,2,0,0,0]]).tolist()
isq.quantumCor.addGate("Rs", a)


#accout to cloud platform
username = "louhuazhe"
password = "lhzmaki1012"
account = Account(username=username, password=password, full_expr_record=True)

#print(isq.quantumCor.gate)

@isq.qpu()
def bell():
    '''
    qbit a, b;
    X<a>;
    CNOT<a, b>;
    M<a>;
    M<b>;
    '''

@isq.qpu()
def test(theta, a):
    '''
    qbit t[5];
    Rs<t[0], t[1], t[2], t[3]>;
    CZ<t[1], t[4]>;
    if (a < 5){
        X<t[a]>;
    }else{
        Y<t[0]>;
    }
    for i in 0:2{
        RX(theta[1])<t[i]>;
    }
    M<t[1]>;
    '''

if __name__ == '__main__':

    # use qpu kernel function, compile and run together
    res = bell()
    print(res)

    # use string, compile and run separately
    with open("test.isq") as f:
        isq_str = f.read()
        # compile to openqasm2.0
        circuit = isq.quantumCor.compileFromStr(isq_str, target="openqasm")
        print(circuit)
        # compile to qcis
        circuit = isq.quantumCor.compileFromStr(isq_str)
        # map or not
        with open("qcis-config.json") as fj:
            data = json.loads(fj.read())
            circuit = isq.quantumCor.getMapping(data['qbit_num'], data['topo'], circuit)
            print(circuit)
        # simulate
        res = isq.quantumCor.simulate(circuit)
        print(res)
        # 12bit
        id = account.submit_job(circuit, exp_name="test", version="1.0")
        if id:
            res = account.query_experiment(id, max_wait_time=10)
            print(res)
